package com.zzf.guigu.Thread;

/**
 * @Author ZZF
 * @Date 2021-08-26-14:54
 */
public class TicketThread {

    public static void main(String[] args) {
        Mythread1 mythread = new Mythread1();
        Thread thread = new Thread(mythread);
        thread.setName("线程1");
        thread.start();
        Thread thread1 = new Thread(mythread);
        thread1.setName("线程2");
        thread1.start();
        Thread thread2 = new Thread(mythread);
        thread2.setName("线程3");
        thread2.start();
    }

}

class Mythread1 implements Runnable {

    private int ticket = 100;

    Object obj = new Object();

    @Override
    public void run() {

        while (true) {
            synchronized (this) {
                if (ticket > 0) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + ":当前是第" + ticket + "张票");
                    ticket--;
                } else {
                    break;
                }
            }
        }
    }
}
